package com.baosight.sczx.zm.cs.service;

import java.util.List;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.EngineeringDesign;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cs.domain.ZMCSEngineering;

/**
 * <p>Title: ServiceZMCS51.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-1 上午11:07:40  张小飞  Create
 *</pre>
 */
public class ServiceZMCS51 extends ServiceEPBase {
	
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化工程信息数据</p>
	 * <p>return value: ServiceZMCSEngineering</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-11下午01:11:47 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		
		//查询区构造下拉框
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.specialFund",true));//专项资金
		outInfo.addBlock(Department.getInstance().getConstructionUnit(true));//施工单位
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.engineeringGroup", true));//工程组
		/**
		EiBlock _tempBlock = WorkingFace.getAllWorkingFace("cmes.cs.engineeringParentId", false);
		_tempBlock.addRows(RoadWay.getAllRoadWay("cmes.cs.engineeringParentId", false).getRows());
		_tempBlock.addRows(OtherDesign.getAllOtherDesign("cmes.cs.engineeringParentId", false).getRows());
		outInfo.addBlock(_tempBlock);
		*/
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p>
	 * <p>return value: ServiceZMCSEngineering</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-11下午01:10:52 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		String tableName = inInfo.getString("tableName");
		String _value = inInfo.getString("value");
		String isLeaf = inInfo.getString("isLeaf");
		
		EiBlock block = inInfo.getBlock(EiConstant.queryBlock) == null ? inInfo.addBlock(EiConstant.queryBlock) : inInfo.getBlock(EiConstant.queryBlock);
		EiInfo outInfo = null;
		
		//点击非叶子节点
		if("0".equals(isLeaf)){
			if("topFaceRoadway".equals(tableName)){
				//工作面巷道树根节点
				List<String> rootNodes = EngineeringDesign.getMineDesign(_value);
				if(rootNodes == null || rootNodes.size() == 0 ){
					return inInfo;
				}
				block.setCell(0, "fdKeyIds", rootNodes);
				outInfo = super.query(inInfo, "ZMCSEngineering.query", new ZMCSEngineering());
			} else if("category".equals(tableName)) {
				//类别二级节点
				String fdType = null;
				if("CM".equals(_value)) {
					fdType = "0";
				} else if("JJ".equals(_value)) {
					fdType = "1";
				} else if("TF".equals(_value)) {
					fdType = "2";
				} else if("QT".equals(_value)) {
					fdType = "3";
				} else {
					fdType = "";
				}
				block.setCell(0, "fdType", fdType);
				outInfo = super.query(inInfo, "ZMCSEngineering.query", new ZMCSEngineering());
			} else {
				if("topUnit".equals(_value)){
					//单位根节点
					block.setCell(0, "fdDepartmentIdColumn", "FD_DEPARTMENT_ID");
					outInfo = super.query(inInfo, "ZMCSEngineering.query", new ZMCSEngineering());
				} else if("topCategory".equals(_value)){
					//类别根节点
					outInfo = super.query(inInfo, "ZMCSEngineering.query", new ZMCSEngineering());
				} else if("topSpecialFund".equals(_value)){
					//专项资金根节点
					block.setCell(0, "fdCapitalSourceColumn", "FD_CAPITAL_SOURCE");
					outInfo = super.query(inInfo, "ZMCSEngineering.query", new ZMCSEngineering());
				} else {
					//工作面巷道树二级节点
					List<String> rootNodes = EngineeringDesign.getTypeDesign(tableName, _value);
					if(rootNodes == null || rootNodes.size() == 0 ){
						return inInfo;
					}
					block.setCell(0, "fdKeyIds", rootNodes);
					outInfo = super.query(inInfo, "ZMCSEngineering.query", new ZMCSEngineering());
				}
			}
		}
		//叶子节点
		else{
			if("unit".equals(tableName)) {
				//单位叶子节点
				block.setCell(0, "fdDepartmentId", _value);
			} else if("CM".equals(tableName)) {
				//采煤类别叶子节点
				block.setCell(0, "fdClass", _value);
				block.setCell(0, "fdType", "0");
			} else if("JJ".equals(tableName)) {
				//掘进类别叶子节点
				block.setCell(0, "fdClass", _value);
				block.setCell(0, "fdType", "1");
			} else if("TF".equals(tableName)) {
				//通防类别叶子节点
				block.setCell(0, "fdClass", _value);
				block.setCell(0, "fdType", "2");
			} else if("QT".equals(tableName)) {
				//其他类别叶子节点
				block.setCell(0, "fdClass", _value);
				block.setCell(0, "fdType", "3");
			} else if("specialFund".equals(tableName)) {
				//专项资金叶子节点
				block.setCell(0, "fdCapitalSource", _value);
			} else {
				//工作面巷道树叶子节点
				block.setCell(0, "fdParentId", _value);
			}
			
			outInfo = super.query(inInfo, "ZMCSEngineering.query", new ZMCSEngineering());
		}

		return outInfo;
	}
}
